/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.port;

import com.Ostermiller.util.CSVPrint;
import com.Ostermiller.util.ExcelCSVPrinter;
import com.google.inject.Inject;
import org.apache.commons.logging.LogFactory;
import org.xptracker.domain.*;
import org.xptracker.persistence.Identified;
import org.xptracker.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public final class CsvExporter {
  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private Domain domain;

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private TypeFactory<Story> storyFields;

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private TypeFactory<User> userFields;

  private class Exporter<T extends Identified> {
    public final void export(Class<T> exportType, CSVPrint printer, List<T> values, TypeFactory<T> types) {
      try {
        final List<String> keys = new ArrayList<String>(types.listTypes());
        for (String key : keys) {
          printer.print(key);
        }
        printer.println();
        for (T t : values) {
          t = domain.fetchEntity(exportType, t.getId());
          LogFactory.getLog(getClass()).info("[" + exportType.getSimpleName() + " Export] Exporting item #" + t.getId());
          for (String key : keys) {
            printer.print(types.makeType(key).withDomain(domain).exportValue(t));
          }
          printer.println();
        }
        printer.close();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }

  public void exportUsers(OutputStream outputStream, List<User> users) {
    new Exporter<User>().export(User.class, createStream(outputStream), users, userFields);
  }

  public void exportStories(OutputStream outputStream, List<Story> stories) {
    new Exporter<Story>().export(Story.class, createStream(outputStream), stories, storyFields);
  }

  public void exportCustomers(ByteArrayOutputStream stream, List<Customer> customers) {
    new Exporter<Customer>().export(Customer.class, createStream(stream), customers, new TypeFactory<Customer>(null) {
      public SimpleType<Customer> makeType(String key) {
        return new SimpleType<Customer>() {
          protected Object getExportValue(Customer customer) {
            return customer.getName();
          }
        };
      }
    });
  }

  private static ExcelCSVPrinter createStream(OutputStream outputStream) {
    return new ExcelCSVPrinter(outputStream);
  }
}



